/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package org.mule.runtime.module.db.internal.result.statement;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mule.runtime.module.db.internal.domain.autogeneratedkey.AutoGeneratedKeyStrategy;
import org.mule.runtime.module.db.internal.domain.connection.DbConnection;
import org.mule.runtime.module.db.internal.domain.param.QueryParam;
import org.mule.runtime.module.db.internal.domain.query.QueryTemplate;
import org.mule.runtime.module.db.internal.domain.query.QueryType;
import org.mule.runtime.module.db.internal.result.resultset.ResultSetHandler;
import org.mule.tck.junit4.AbstractMuleTestCase;

import java.sql.Statement;
import java.util.Collections;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

/**
 * Base class for {@link StatementResultHandler} tests
 */
public abstract class AbstractStatementResultHandlerTestCase extends AbstractMuleTestCase {

  private final QueryTemplate queryTemplate = new QueryTemplate("UNUSED", QueryType.SELECT, Collections.<QueryParam>emptyList());
  private final AutoGeneratedKeyStrategy autoGeneratedKeyStrategy = mock(AutoGeneratedKeyStrategy.class);
  private final DbConnection connection = mock(DbConnection.class);
  private final Statement statement = mock(Statement.class);
  private final ResultSetHandler resultSetHandler = mock(ResultSetHandler.class);
  private final StatementResultHandler statementResultHandler = createStatementResultHandler(resultSetHandler);
  private final StatementResultIterator statementResultIterator = mock(StatementResultIterator.class);
  private final StatementResultIteratorFactory statementResultIteratorFactory = mock(StatementResultIteratorFactory.class);

  @Before
  public void setUpStatementResultIteratorFactory() throws Exception {
    when(statementResultIteratorFactory.create(connection, statement, queryTemplate, autoGeneratedKeyStrategy))
        .thenReturn(statementResultIterator);
    when(connection.getStatementResultIteratorFactory(resultSetHandler)).thenReturn(statementResultIteratorFactory);
  }

  @Test
  public void processEmptyStatementResults() throws Exception {
    Object statementResults =
        statementResultHandler.processStatement(connection, statement, queryTemplate, autoGeneratedKeyStrategy);

    assertThat(statementResults, is(instanceOf(Map.class)));
    Map mapResults = (Map) statementResults;
    assertThat(mapResults.size(), equalTo(0));
  }

  @Test
  public void processMultipleStatementResults() throws Exception {
    StatementResult result1 = mock(StatementResult.class);
    when(result1.getName()).thenReturn("result1");
    StatementResult result2 = mock(StatementResult.class);
    when(result2.getName()).thenReturn("result2");

    when(statementResultIterator.hasNext()).thenReturn(true, true, false);
    when(statementResultIterator.next()).thenReturn(result1, result2);


    Object statementResults =
        statementResultHandler.processStatement(connection, statement, queryTemplate, autoGeneratedKeyStrategy);

    assertThat(statementResults, is(instanceOf(Map.class)));
    Map mapResults = (Map) statementResults;
    assertThat(mapResults.size(), equalTo(2));
    assertThat(mapResults.containsKey("result1"), is(true));
    assertThat(mapResults.containsKey("result2"), is(true));
  }

  @Test
  public void closesResourcesIfNeeded() throws Exception {
    statementResultHandler.processStatement(connection, statement, queryTemplate, autoGeneratedKeyStrategy);

    int expectedTimes = mustCloseResources() ? 1 : 0;

    verify(statement, times(expectedTimes)).close();
    verify(connection, times(expectedTimes)).release();
  }

  protected abstract StatementResultHandler createStatementResultHandler(ResultSetHandler resultSetHandler);

  protected abstract boolean mustCloseResources();
}
